package org.example.ch05;


/**
 * @author yang
 * @date 2024-03-20
 */
public class MyThread extends Thread {
    private static int number;

    public MyThread(int number) {
        this.number = number;
    }

    public MyThread() {
    }

    @Override
    public void run() {
        while (true){
            synchronized (MyThread.class){
                if (number > 0){
                    number--;
                    System.out.println(Thread.currentThread().getName()+"卖了1张票,还剩"+number+"张");
                    try {
                        Thread.sleep(500);
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                }else{
                    System.out.println(Thread.currentThread().getName()+"处电影票售罄");
                    break;
                }
            }
        }
    }
}
